//
//  YbrPerferenceTool.m
//  wifibox
//
//  Created by os X 10.9 on 14-11-9.
//  Copyright (c) 2014年 yinbaner. All rights reserved.
//  这个是数据存储的地方

#define mFileName @"data.plist"

#import "YbrPerferenceTool.h"
#import "NSString+Ybr.h"

@interface YbrPerferenceTool ()
//@property (nonatomic,strong) NSMutableDictionary *dict;
@end

@implementation YbrPerferenceTool

static NSMutableDictionary *dict;

+(YbrPerferenceTool *) prefer {
    @synchronized(self)
    {
//        if(dict == nil) {
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentDirectory = [paths objectAtIndex:0];
            NSString *filePath = [documentDirectory stringByAppendingPathComponent:mFileName];
        
            if ([[NSFileManager defaultManager]fileExistsAtPath:filePath]) {
                dict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
            }
            
            if(dict == nil) {
                dict = [NSMutableDictionary dictionary]; //初始化空的可变字典
            }
//        }
        
        return [[YbrPerferenceTool alloc] init];
    }
}


//将kFileName串联到Documents目录的路径，以创建并返回数据文件的完整路径名
-(NSString *)dataFilePath
{
    /*常量NSDocumentDirectory表明我们正在查找Documents目录路径，第二个常量NSUserDomainMask表示的是把搜索范围定在应用程序沙盒中，YES表示的是希望希望该函数能查看用户主目录*/
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //  数组索引0处Documentd目录，
    NSString *documentDirectory = [paths objectAtIndex:0];
    //  返回一个kFileName的完整路径
    return [documentDirectory stringByAppendingPathComponent:mFileName];
}

-(void) edited {
    [dict writeToFile:[self dataFilePath] atomically:YES];
}

//获取元素
-(id) getPerValue:(NSString *)str defule:(id) defule{
    
    id content = [dict objectForKey:str];
    if(content == nil) {
        return defule;
    }
    return content;
}

//设置元素
-(void) setPerValue:(id) str _id:(NSString *)_id {
    if(str == nil) {
        [dict removeObjectForKey:_id];
        return;
    }
    
//    NSLog(@"-");
    if(dict == nil){
        NSLog(@"-----s-df-asd-fa-sdf-a-dsf-asd-fa-sd-fa-d-fa-s---");
    }
    
    
    
    NSLog(@"----ceshiceshicehsi-----%@----------%@",str,_id);
    
    
    
    [dict setObject:str forKey:_id];
    
    
    NSLog(@"---------hhhhhhhhh----------%@----------",[dict objectForKey:_id]);
    
    
    
    
}

// 设置用户ID
-(void) setUserId:(long) _id {
    NSNumber *userid = [NSNumber numberWithLong:_id];
    [self setPerValue:userid _id:@"userid"];
}

//获取用户的id
-(long) getUserId:(long) defule {
    NSNumber *userid = [self getPerValue:@"userid" defule:[NSNumber numberWithLong:defule]];
    return [userid longValue];
}

// 设置用户名
-(void) setUserName:(NSString *) username {
    [self setPerValue:username _id:@"username"];
}

// 获取用户名
-(NSString *) getUserName:(NSString *) defule {
    return [self getPerValue:@"username" defule:defule];
}

// 设置临时的存放已经成功的注册用户手机号(也就是已经发了短信的)
-(void) setRegUsePhoneTemp:(NSString *) phone {
    [self setPerValue:phone _id:@"reguserPhoneTemp"];
}

// 获取用户的手机号码
-(NSString *) getRegUsePhoneTemp:(NSString *) defule {
    return [self getPerValue:@"reguserPhoneTemp" defule:defule];
}

// 注册时间 (为了防止同一时间内多次注册)
-(void) setRegTimer:(NSTimeInterval) time {
    NSNumber *doubleNumber = [NSNumber numberWithDouble:time];
    [self setPerValue:doubleNumber _id:@"time"];
}

//获取用户的注册时间
-(NSTimeInterval) getRegTimer:(NSTimeInterval) defule {
    NSNumber *doubleNumber = [self getPerValue:@"time" defule:[NSNumber numberWithDouble:defule]];
    return [doubleNumber doubleValue];
}

// 注册所有的短信数量
-(void) setRegMsmNum:(int)msmnum {
    NSNumber *regmsm = [NSNumber numberWithInt:msmnum];
    [self setPerValue:regmsm _id:@"regmsm"];
}

// 获取注册时发送的短信数量
-(int) getRegMsmNum:(int) num {
    NSNumber *regmsm = [self getPerValue:@"regmsm" defule:[NSNumber numberWithInt:0]];
    return [regmsm intValue];
}

// 找回密码用的短信数量
-(void) setFindPswMsmNum:(int)msmnum {
    NSNumber *regmsm = [NSNumber numberWithInt:msmnum];
    [self setPerValue:regmsm _id:@"findmsm"];
}

// 获取找回密码是已使用了多少条短信
-(int) getFindPswMsmNum:(int) num {
    NSNumber *regmsm = [self getPerValue:@"findmsm" defule:[NSNumber numberWithInt:num]];
    return [regmsm intValue];
}

// 设置语文的版本
-(void) setGradeChina:(id) baseHttp {
    [self setPerValue:baseHttp _id:@"grade_china"];
}

// 获取语文年级
-(YbrBaseObject *) getGradeChina {
    id baseStr = [self getPerValue:@"grade_china" defule:nil];
    if(baseStr == nil) {
        return nil;
    }
    return [YbrBaseObject BaseObjectWithDict:baseStr];
}

// 设置英语的版本
-(void) setGradeEnglish:(id) baseHttp {
    [self setPerValue:baseHttp _id:@"grade_english"];
}

// 获取英语的年级
-(YbrBaseObject *) getGradeEnglish {
    id baseStr = [self getPerValue:@"grade_english" defule:nil];
    if(baseStr == nil) {
        return nil;
    }
    return [YbrBaseObject BaseObjectWithDict:baseStr];
}


// 设置httpService的url
-(void) setHttpServiceUri:(NSString *) uri {
    [self setPerValue:uri _id:@"http_service_uri"];
}

// 获取httpService的url
-(NSString *) getHttpServiceUri {
    NSString *baseStr = [self getPerValue:@"http_service_uri" defule:nil];
    return baseStr;
}

// 设置选择的设备到前面
-(void) setSeleDeviceToPre:(NSString *) device {
    [self setPerValue:device _id:@"dlna_sele_device"];
}

// 获取选择的设备的
-(YbrDevice *) getSeleDeviceFromPre {
     NSString *baseStr = [self getPerValue:@"dlna_sele_device" defule:nil];
    if([NSString isEmpty:baseStr]) {
        return nil;
    }
    
    id mydevice = [NSJSONSerialization JSONObjectWithData:[baseStr dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
    YbrDevice *device               =   [[YbrDevice alloc] init];
    device.DeviceName               =   mydevice[@"DeviceName"];
    device.DeviceIpAddr             =   mydevice[@"DeviceIpAddr"];
    device.DeviceFiremwareVersion   =   mydevice[@"DeviceFiremwareVersion"];
    device.HttpApiPort              =   mydevice[@"HttpApiPort"];
    device.addTime                  =   [[NSDate date] timeIntervalSinceReferenceDate];
    return device;
}

// 设置选择设备的IP
-(void) setSeleDeviceIP:(NSString *) ctrIP {
    [self setPerValue:ctrIP _id:@"select_device_ip"];
}

// 获取选择设备的ip
-(NSString *) getSeleDeviceIP {
    NSString *baseStr = [self getPerValue:@"select_device_ip" defule:nil];
    return baseStr;
}

// 设置发生了改变
-(void) setChanngeChannel:(BOOL) isChange {
    NSNumber *ischange = [NSNumber numberWithBool:isChange];
    [self setPerValue:ischange _id:@"isChangeChannel"];
}

// 是否发生改变
-(BOOL) isChanngeChannel {
    NSNumber *ischange = [self getPerValue:@"isChangeChannel" defule:[NSNumber numberWithBool:NO]];
    return [ischange boolValue];
}

// 设置SN码
-(void) setSNString:(NSString *) ns {
    [self setPerValue:ns _id:@"my_device_ns"];
}

// 获取SN码
-(NSString *) getSNString {
    NSString *baseStr = [self getPerValue:@"my_device_ns" defule:nil];
    return baseStr;
}

// 设置设备的ID
-(void) setDeviceID:(NSString *) ns {
    [self setPerValue:ns _id:@"device_id"];
}

// 获取设备的ID
-(NSString *) getDeviceID {
    NSString *baseStr = [self getPerValue:@"device_id" defule:nil];
    return baseStr;
}

// 记录最新固件版本
-(void) setHardFireVersion:(NSString *) version {
    [self setPerValue:version _id:@"hardfire_version"];
}

// 获取最新固件版本
-(NSString *) getHardFireVersion{
    NSString * version = [self getPerValue:@"hardfire_version" defule:nil];
    return version;
}


// 记录最新固件版本url
-(void) setHardFireUrl:(NSString *) url {
    [self setPerValue:url _id:@"hardfire_url"];
}

// 获取最新固件版本url
-(NSString *) getHardFireUrl{
    NSString * version = [self getPerValue:@"hardfire_url" defule:nil];
    return version;
}


//=================================================================================
// 记录用户详细信息
-(void) setUserDetails:(NSString *) datas
{
    [self setPerValue:datas _id:@"userDetail"];
}

// 获取用户登录的个人详细信息
-(NSString *) getUserDetails
{
    NSString * userDatas = [self getPerValue:@"userDetail" defule:nil];
    return userDatas;
}

// 记录用户的同步信息: 我的书包,我喜欢的,播放历史,我的消息
-(void) setUserSynchronizeDatas:(NSString *) datas
{
    [self setPerValue:datas _id:@"userSynchronizeDatas"];
}

// 获取用户同步下来的数据: 我的书包,我喜欢的,播放历史,我的消息
-(NSString *) getUserSynchronizeDatas{
    NSString * userDatas = [self getPerValue:@"userSynchronizeDatas" defule:nil];
    return userDatas;
}


// 记录用户的聊天唯一标识
-(void) setUserChatName:(NSString *) value
{
    [self setPerValue:value _id:@"ChatName"];
}
-(NSString *) getUserChatName
{
    NSString * userDatas = [self getPerValue:@"ChatName" defule:@""];
    return userDatas;
}

// 记录用户的身份,角色
-(void) setUserCharacterId:(int)value
{
    NSNumber *regmsm = [NSNumber numberWithInt:value];
     [self setPerValue:regmsm _id:@"CharacterId"];
}

-(int) getUserCharacterId
{
//    NSString * userDatas = [self getPerValue:@"CharacterId" defule:nil];
//    return userDatas;
    
    NSNumber *regmsm = [self getPerValue:@"CharacterId" defule:[NSNumber numberWithInt:0]];
    return [regmsm intValue];
    
}

/// 获取APP首页的信息
-(void) setAppMainPage:(NSString *)datas
{
    [self setPerValue:datas _id:@"MainPage"];
}

-(NSString *)getAppMainPage
{
    NSString * userDatas = [self getPerValue:@"MainPage" defule:nil];
    return userDatas;
}


// 记录用户的身份,角色
-(void) setUserNickName:(NSString *) value
{
    [self setPerValue:value _id:@"NickName"];
}

-(NSString *) getUserNickName
{
    NSString * userDatas = [self getPerValue:@"NickName" defule:nil];
    return userDatas;
}


// 记录用户的地理位置
// 省份
-(void) setProvince:(NSString *) value
{
    [self setPerValue:value _id:@"mProvince"];
}

-(NSString *) getProvince:(NSString *)defaults
{
    NSString * province = [self getPerValue:@"mProvince" defule:defaults];
    return province;
}

// 城市
-(void) setCity:(NSString *) value
{
    [self setPerValue:value _id:@"mCity"];
}

-(NSString *) getCity:(NSString *)defaults
{
    NSString * city = [self getPerValue:@"mCity" defule:defaults];
    return city;
}

// 地区
-(void) setDistrict:(NSString *) value
{
    [self setPerValue:value _id:@"mDistrict"];
}

-(NSString *) getDistrict:(NSString *)defaults
{
    NSString * district = [self getPerValue:@"mDistrict" defule:defaults];
    return district;
}



// 注册所有的短信数量
//-(void) setRegMsmNum:(int)msmnum {
//    NSNumber *regmsm = [NSNumber numberWithInt:msmnum];
//    [self setPerValue:regmsm _id:@"regmsm"];
//}
//
//// 获取注册时发送的短信数量
//-(int) getRegMsmNum:(int) num {
//    NSNumber *regmsm = [self getPerValue:@"regmsm" defule:[NSNumber numberWithInt:0]];
//    return [regmsm intValue];
//}



// MessageCount
-(void)setMessageCount: (int)count
{
    NSNumber *regmsm = [NSNumber numberWithInt:count];
    [self setPerValue:regmsm _id:@"mMessageCount"];
}

-(int)getMessageCount:(int)defaults
{
    NSNumber *regmsm = [self getPerValue:@"mMessageCount" defule:[NSNumber numberWithInt:0]];
    return [regmsm intValue];
}

// chatCount
-(void)setChatCount: (int)count
{
    NSNumber *regmsm = [NSNumber numberWithInt:count];
    [self setPerValue:regmsm _id:@"mChatCount"];
}
-(int)getChatCount:(int)defaults
{
    NSNumber *regmsm = [self getPerValue:@"mChatCount" defule:[NSNumber numberWithInt:0]];
    return [regmsm intValue];
}

// searchCount
-(void)setSearchCount: (int)count
{
    NSNumber *regmsm = [NSNumber numberWithInt:count];
    [self setPerValue:regmsm _id:@"mSearchCount"];
}
-(int)getSearchCount:(int)defaults
{
    NSNumber *regmsm = [self getPerValue:@"mSearchCount" defule:[NSNumber numberWithInt:0]];
    return [regmsm intValue];
}

// userCount
-(void)setUserCount: (int)count
{
    NSNumber *regmsm = [NSNumber numberWithInt:count];
    [self setPerValue:regmsm _id:@"mUserCount"];
}
-(int)getUserCount:(int)defaults
{
    NSNumber *regmsm = [self getPerValue:@"mUserCount" defule:[NSNumber numberWithInt:0]];
    return [regmsm intValue];
}


// 是否显示昵称
-(void)setIMGroupIsShowNickName: (int)value
{
    NSNumber *regmsm = [NSNumber numberWithInt:value];
    [self setPerValue:regmsm _id:@"mImGroupIsShowNickName"];
}

-(int)getImGroupIsShowNickName:(int)defaults
{
    NSNumber *regmsm = [self getPerValue:@"mImGroupIsShowNickName" defule:[NSNumber numberWithInt:0]];
    return [regmsm intValue];
}











// 打印
-(void) log {
    NSLog(@"打印下信息   %@" ,dict);
}

@end
